package com.travelsky.airchina.boarding.dao.impl.hibernate;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.travelsky.airchina.boarding.dao.MemberDAO;
import com.travelsky.airchina.boarding.domain.Member;

@Repository("memberDAO")
public class MemberDAOImpl extends GenericDAOHibernate<Member,Long,MemberDAO> implements MemberDAO {
	@Autowired
	public MemberDAOImpl(SessionFactory sessionFactory){
		this.setSessionFactory(sessionFactory);
	}

	public boolean isExistLoginName(String loginName) {
		String hql = "select count(id) from Member where loginName = :loginName";
		Map<String,String> params = new HashMap<String,String>();
		params.put("loginName", loginName);
		int sum = this.countHqlQuery(hql, params);
		return sum>0?true:false;
	}

	public boolean isExistEmail(String email) {
		String hql = "select count(id) from Member where email = :email";
		Map<String,String> params = new HashMap<String,String>();
		params.put("email", email);
		int sum = this.countHqlQuery(hql, params);
		return sum>0?true:false;
	}

	@SuppressWarnings("unchecked")
	public Member getMember(String loginName) {
		String hql = "from Member where loginName = ?";
		List<Member> list = this.getHibernateTemplate().find(hql,loginName);
		if(list!=null && list.size()>0){
			return list.get(0);
		}
		return null;
	}

}
